﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using ControleRepresentante.Logic.Models;

namespace WebSite.ViewModels
{
    public enum ButtonType
    {
        Edit,
        Delete,
        Duplicate,
        Cancel,
        Insert,
        Print,
        AddUser,
        Search,
        Login,
        CreatePedido
    }

    public class ButtonsViewModel : IHtmlString
    {
        private readonly HtmlHelper m_HtmlHelper;
        private readonly List<ButtonItemBase> m_Buttons;

        public IEnumerable<ButtonItemBase> Buttons { get { return m_Buttons; } }

        public ButtonsViewModel(HtmlHelper htmlHelper)
        {
            m_HtmlHelper = htmlHelper;
            m_Buttons = new List<ButtonItemBase>();
        }

        public ButtonsViewModel Add(ModelBase model, string controllerName, params ButtonType[] buttonTypes)
        {
            foreach (ButtonType buttonType in buttonTypes)
            {
                m_Buttons.Add(new ButtonTypeItem(buttonType, model, controllerName));
            }
            return this;
        }

        public ButtonsViewModel Add(ButtonType buttonType, string href, string caption, string onClick = null)
        {
            m_Buttons.Add(new ButtonItem(buttonType, href, caption, onClick));
            return this;
        }

        public IHtmlString Render()
        {
            return m_HtmlHelper.Partial("Buttons", this);
        }

        public string ToHtmlString()
        {
            return Render().ToHtmlString();
        }
    }

    public class ButtonTypeItem : ButtonItemBase
    {
        private readonly ButtonType m_ButtonType;
        private readonly ModelBase m_Model;
        private readonly string m_ControllerName;

        public ButtonTypeItem(ButtonType buttonType, ModelBase model, string controllerName)
            : base(buttonType, GetCaption(buttonType))
        {
            m_ButtonType = buttonType;
            m_Model = model;
            m_ControllerName = controllerName;
        }

        private static string GetCaption(ButtonType buttonType)
        {
            switch (buttonType)
            {
                case ButtonType.Edit:
                    return "Editar";
                case ButtonType.Delete:
                    return "Excluir";
                case ButtonType.Duplicate:
                    return "Duplicar";
                case ButtonType.Cancel:
                    return "Cancelar";
                case ButtonType.Print:
                    return "Imprimir";
                case ButtonType.AddUser:
                    return "Criar Usuário";
                default:
                    throw new ArgumentException("Invalid buttonType" + buttonType, "buttonType");
            }
        }

        public override string GetHRef(UrlHelper urlHelper)
        {
            string actionName = GetActionName(m_ButtonType);
            return urlHelper.Action(actionName, m_ControllerName, new { id = m_Model.Id });
        }

        private static string GetActionName(ButtonType buttonType)
        {
            string actionName;
            switch (buttonType)
            {
                case ButtonType.Edit:
                    actionName = "Edit";
                    break;
                case ButtonType.Delete:
                    actionName = "Delete";
                    break;
                case ButtonType.Duplicate:
                    actionName = "Duplicate";
                    break;
                case ButtonType.Cancel:
                    actionName = "Details";
                    break;
                case ButtonType.Print:
                    actionName = "Print";
                    break;
                case ButtonType.AddUser:
                    actionName = "AddUser";
                    break;
                default:
                    throw new ArgumentException("Invalid buttonType" + buttonType, "buttonType");
            }
            return actionName;
        }
    }

    public abstract class ButtonItemBase
    {
        protected ButtonItemBase(ButtonType buttonType, string caption, string onClick = null)
        {
            SetClass(buttonType);
            Target = buttonType == ButtonType.Print ? "_blank" : "_self";
            Caption = caption;
            OnClick = onClick;
        }

        public string AClass { get; private set; }
        public string Caption { get; private set; }
        public string OnClick { get; private set; }
        public string Target { get; private set; }
        public string SpanClass { get; private set; }

        public abstract string GetHRef(UrlHelper urlHelper);

        private void SetClass(ButtonType buttonType)
        {
            string className;
            switch (buttonType)
            {
                case ButtonType.Edit:
                    className = "edit";
                    break;
                case ButtonType.Delete:
                    className = "delete";
                    break;
                case ButtonType.Duplicate:
                    className = "duplicate";
                    break;
                case ButtonType.Cancel:
                    className = "cancel";
                    break;
                case ButtonType.Insert:
                    className = "insert";
                    break;
                case ButtonType.Print:
                    className = "print";
                    break;
                case ButtonType.AddUser:
                    className = "adduser";
                    break;
                case ButtonType.Search:
                    className = "search";
                    break;
                case ButtonType.Login:
                    className = "login";
                    break;
                default:
                    throw new ArgumentException("Invalid buttonType" + buttonType, "buttonType");
            }
            switch (buttonType)
            {
                case ButtonType.Duplicate:
                case ButtonType.Print:
                case ButtonType.AddUser:
                case ButtonType.Search:
                case ButtonType.Login:
                    AClass = "t-button t-button-icon";
                    SpanClass = className;
                    break;
                default:
                    AClass = String.Format("t-button t-grid-{0} t-button-icon", className);
                    SpanClass = String.Format("t-icon t-{0}", className);
                    break;
            }
        }
    }

    public class ButtonItem : ButtonItemBase
    {
        private readonly string m_Href;

        public ButtonItem(ButtonType buttonType, string href, string caption, string onClick = null)
            : base(buttonType, caption, onClick)
        {
            m_Href = href;
        }

        public override string GetHRef(UrlHelper urlHelper)
        {
            return m_Href;
        }
    }
}